import { reactive, inject } from "vue";
import { useFlowStore } from '../../store/useFlowStore'

export const useFlowNodeTable = () => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;
    const flowNodeTable = reactive(
        {
            total: 0,
            tableData: [],
            flowNodeApproveUsers: [],
            flowStartNode: null,
            nodeOptions: [
                {
                    value: 1,
                    label: '专人审批',
                },
                {
                    value: 2,
                    label: '角色审批',
                },
                {
                    value: 3,
                    label: '直属上级审批',
                },
                {
                    value: 4,
                    label: '其他',
                }
            ],
            passConditions: [
                {
                    value: 0,
                    label: '所有人通过',
                },
                {
                    value: 1,
                    label: '一人通过',
                },
            ],
        }
    );

    const flowStore = useFlowStore();

    //查询
    const getFlowNode = async (flowId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Flow.GeFlowNodeByFlowId,
                {
                    flowId: flowId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                flowNodeTable.tableData = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    };
    //流程启动节点
    const getFlowStartNode = async (flowId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Flow.GeFlowStartNodeByFlowId,
                {
                    flowId: flowId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                flowNodeTable.flowStartNode = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    };
    //新增
    const addData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Flow.CreateNode,
                flowStore.flowNodeModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新
    const updateData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Flow.UpdateNode,
                flowStore.flowNodeModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //删除
    const deleteData = async (flowNodeId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Flow.DeleteByFlowNodeId,
                { flowNodeId: flowNodeId }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //获取专人审批节点对应的审批人
    const getFlowNodeApproveUser = async (nodeId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Flow.GetApproveUserByNodeId,
                {
                    nodeId: nodeId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                flowNodeTable.flowNodeApproveUsers = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    };

    //获取角色审批节点对应的审批人
    const getFlowNodeApproveUserByRoleId = async (roleId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.User.GetUserByRoleId,
                {
                    roleId: roleId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                flowNodeTable.flowNodeApproveUsers = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    };
    return { flowNodeTable, flowStore, getFlowNode, getFlowStartNode, addData, updateData, deleteData, getFlowNodeApproveUser, getFlowNodeApproveUserByRoleId };
};